import tkinter as tk
import smoke
from tkinter import Label
from PIL import Image, ImageTk
import time

# 创建主窗口
window = tk.Tk()
window.title("抽奖模拟器")
window.geometry("1270x680")

label = tk.Label(window, text="狐狐天下第一抽奖模拟器", fg="red", font=("手书体", 30))
label.pack()
# 抽奖函数
def draw_prize():
    prize = smoke.smoke_lottery(smoke.prizes)
    if prize:
        show_prize_image(window, prize)


# 单次抽奖按钮
single_draw_button = tk.Button(window, text="单抽(50000银狮)", command=draw_prize,width=20, height=1)
single_draw_button.pack()
single_draw_button.place(x=380, y=100)

# 连续抽奖10次按钮
def ten_draws():
    for _ in range(10):
        draw_prize()

ten_draws_button = tk.Button(window, text="十连(500000银狮)", command=ten_draws, width=20, height=1)
ten_draws_button.pack()
ten_draws_button.place(x=780, y=100)

#清屏按钮
clear_button = tk.Button(window, text="清屏", command=lambda: clear_frame(window), width=20, height=1)
clear_button.pack()
clear_button.place(x=100, y=100)

# 显示图片函数
x_position=100
y_position=150

def show_prize_image(window, prize_info):
    image_path = prize_info['image_path']
    global x_position   # 初始X坐标
    global y_position # Y坐标

    try:
        image = Image.open(image_path)

        resized_image = image.resize((200, 200), Image.Resampling.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        # 创建Frame来包含Label
        frame = tk.Frame(window)
        frame.place(x=x_position, y=y_position)

        # 创建Label
        label = Label(frame, image=photo)
        label.image = photo
        label.pack()

        # 更新X坐标以显示下一个图片
        x_position += 220  # 每张图片之间留出一定间距

        if x_position >= 1200:
            x_position = 100
            y_position += 220

        if y_position > 370:
            y_position = 150


    except Exception as e:
        print(f"加载图像失败: {e}")


def clear_frame(frame, keep_widgets=None):
    global x_position
    global y_position
    time.sleep(0.5)
    if keep_widgets is None:
        keep_widgets = [label,single_draw_button,ten_draws_button,clear_button]

    # 记录需要保留的小部件
    widgets_to_keep = [w for w in frame.winfo_children() if w in keep_widgets]

    # 销毁其他小部件
    for widget in frame.winfo_children():
        if widget not in widgets_to_keep:
            widget.destroy()
    return x_position,y_position

x = str(x_position)
y = str(y_position)

with open("log.txt", "a", encoding="utf-8") as f:
    f.write(f"程序启动时间: {time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}\n")
    f.write(f"程序启动位置: ({x},{y})\n")
    f.write(f"version 1.0.1\n")
    f.write(f"------------------------------------------------------\n")

window.mainloop()
# 更新日志：
'''
未来的计划：
将导入银狮作为通用货币
银狮获得方法：参与战斗，抽奖
'''